package com.atguigu.gulimall.order.config;

import com.atguigu.common.vo.MemberRespVo;
import com.atguigu.gulimall.order.interceptor.LoginUserInterceptor;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 马迎迎
 * @Title: GuliFeignConfig
 * @Package com.atguigu.gulimall.order.config
 * @Description: TODO(添加描述)
 * @date 2021年05月29日 16:26
 */
@Configuration
public class GuliFeignConfig {
    @Bean
    public RequestInterceptor requestInterceptor(){
        return new RequestInterceptor(){
            @Override
            public void apply(RequestTemplate requestTemplate) {
                //1.RequestContextHolder拿到调用这个远程的请求
                ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
                if(requestAttributes!=null){
                    HttpServletRequest request = requestAttributes.getRequest();//老请求
//                System.out.println("feign远程之前先执行RequestTemplate.apply");
                    if(request!=null){
                        //2.同步请求头数据 cookie
                        String cookie = request.getHeader("Cookie");
                        //给新请求同步了老请求的cookie
                        requestTemplate.header("Cookie",cookie);
                    }
                }



            }
        };
    }
}
